package com.atguigu.lease.web.admin.interceptor;

import com.atguigu.lease.common.exception.EnumException;
import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author 86188
 * @version v1.0
 * @project ShangTingApartment
 * @Date 2025/7/13 17:34
 * @company []
 * @description
 */

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取token（请求头，access_token）
        String accessToken = request.getHeader("access_token");
        if (ObjectUtils.isEmpty(accessToken)){
            throw new EnumException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        //2.有token，检查是否过期
        JwtUtil.parseToken(accessToken);
        return true;
    }
}
